<?php

/**
 * Class YFormActions
 */
class YFormActions extends CWidget
{
	/** @var  CActiveRecord */
	public $model;
	/** @var  Channel */
	public $channel;
	public $btnSave = true;
	public $btnAddAnother = true;
	public $btnContinue = true;
	public $btnDelete = true;


	public function init()
	{
		parent::init();
		if (!isset($this->model) || !$this->model instanceof CActiveRecord) {
			throw new CException('模型未设置');
		}
	}

	public function run()
	{
		$buttons = array();
		if ($this->btnSave) {
			if ($this->model->getIsNewRecord()) {
				$label = '<i class="fa fa-plus"></i> 创建';
			} else {
				$label = '<i class="fa fa-save"></i> 保存';
			}
			$buttons[] = '<button name="_save" class="btn btn-primary" type="submit">' . $label . '</button>';
		}

		if ($this->btnAddAnother) {
			if ($this->model->getIsNewRecord()) {
				$label = '创建并增加另一个';
			} else {
				$label = '保存并增加另一个';
			}
			$buttons[] = '<button name="_addAnother" class="btn" type="submit">' . $label . '</button>';
		}

		if ($this->btnContinue) {
			if ($this->model->getIsNewRecord()) {
				$label = '创建并继续更新';
			} else {
				$label = '保存并继续更新';
			}
			$buttons[] ='<button name="_continue" class="btn" type="submit">' . $label . '</button>';
		}

		if ($this->btnDelete && !$this->model->getIsNewRecord()) {
			$submit = array('delete', 'id' => $this->model->getPrimaryKey());
			if (isset($this->channel)) {
				$submit['channel_id'] = $this->channel->id;
			}
			$htmlOptions = array(
				'class' => 'pull-right btn btn-danger',
				'csrf' => true,
				'submit' => $submit,
				'confirm' => '确定要删除这条数据吗?');
			$buttons[] = CHtml::link('<i class="fa fa-trash-o"></i> 删除', '#', $htmlOptions);
		}

		echo implode("\n", $buttons);
	}
}
